package dnl.dev.memlog.parser;

import java.io.File;
import java.io.FilenameFilter;
import java.util.Arrays;

import ajul.lang.Args;
import dnl.dev.memlog.sampler.MemSampler;

public class HistogramsDir {

	private File dir;

	public HistogramsDir(File dir) {
		Args.assertNotNull(dir);
		this.dir = dir;
	}

	public File[] listHistogramFiles(){
		File[] histoFiles = dir.listFiles(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".mem.histo");
			}
		});
		return histoFiles;
	}

	public String[] listHistogramFileNames(){
		if(!dir.exists()){
			return new String[]{};
		}
		String[] histoFileNames = dir.list(new FilenameFilter() {
			@Override
			public boolean accept(File dir, String name) {
				return name.endsWith(".mem.histo");
			}
		});
		Arrays.sort(histoFileNames);
		return histoFileNames;
	}

	public File getHistogramFile(String fname){
		File f = new File(dir, fname);
		if(f.exists()){
			return f;
		}
		f = new File(dir, fname+MemSampler.HISTO_FILE_EXT);
		if(!f.exists()){
			System.err.println("No file matching: "+fname);
		}
		return f;
	}
}
